package com.lyhdev.android.touchenglish;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class TouchEnglish extends Activity implements OnClickListener {

	private final static String TAG = "TouchEnglish";

	private static int index = 0;

	private static ServiceFactory service = ServiceFactory.getInstance();

	private TEChannel channel;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		initButtons();
		initUIInteractions();

		channel = service.channel("toeic_demo");
		show();
	}

	private void initButtons() {
		Button buttonView = (Button) findViewById(R.id.ButtonView);
		buttonView.setOnClickListener(this);

		Button buttonPlay = (Button) findViewById(R.id.ButtonPlay);
		buttonPlay.setOnClickListener(this);

		Button buttonSubmit = (Button) findViewById(R.id.ButtonSubmit);
		buttonSubmit.setOnClickListener(this);

		Button buttonPrev = (Button) findViewById(R.id.ButtonPrev);
		buttonPrev.setOnClickListener(this);

		Button buttonNext = (Button) findViewById(R.id.ButtonNext);
		buttonNext.setOnClickListener(this);
	}

	private void initUIInteractions() {
	}

	public void show() {
		TEQuiz quiz = channel.getQuiz(index);
		if (quiz != null) {
			// show image
			if (quiz.getImageFile() != null) {
				ImageView image = (ImageView) findViewById(R.id.ImageViewBack);
				image.setImageBitmap(BitmapFactory.decodeFile(quiz
						.getImageFile().getAbsolutePath()));
			}

			// show text
			TextView text = (TextView) findViewById(R.id.TextView01);
			text.setText(quiz.getText());

			// show options
			int c = 0;
			for (String option : quiz.getOptions()) {
				CheckBox checkbox = null;
				switch (c) {
				case 0:
					checkbox = (CheckBox) findViewById(R.id.CheckBox01);
					break;
				case 1:
					checkbox = (CheckBox) findViewById(R.id.CheckBox02);
					break;
				case 2:
					checkbox = (CheckBox) findViewById(R.id.CheckBox03);
					break;
				case 3:
					checkbox = (CheckBox) findViewById(R.id.CheckBox04);
					break;
				}
				if (checkbox != null) {
					checkbox.setText(option);
				}
				c++;
			}

			// process buttons display

			Button buttonPrev = (Button) findViewById(R.id.ButtonPrev);
			if (index == 0) {
				buttonPrev.setEnabled(false);
			} else {
				buttonPrev.setEnabled(true);
			}

			Button buttonNext = (Button) findViewById(R.id.ButtonNext);
			if (index == (channel.getQuizSize() - 1)) {
				buttonNext.setEnabled(false);
			} else {
				buttonNext.setEnabled(true);
			}

			// process progress display
			TextView textProgress = (TextView) findViewById(R.id.TextViewProgress);
			int current = index + 1;
			int total = channel.getQuizSize();
			textProgress.setText(String.format("%s/%s", current, total));

			// play sound
			playSound();
		}
	}

	private void playSound() {
		service.playSound(channel.getQuiz(index));
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ButtonView:
			Intent intent = new Intent();
			intent.setClass(TouchEnglish.this, ViewImageActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString("imagePath", channel.getQuiz(index).getImageFile()
					.getAbsolutePath());
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case R.id.ButtonPlay:
			playSound();
			break;
		case R.id.ButtonSubmit:
			

			
			TEQuiz quiz = channel.getQuiz(index);
			if (quiz != null) {
				Boolean result = true;
				
				CheckBox checkbox01 = (CheckBox) findViewById(R.id.CheckBox01);
				CheckBox checkbox02 = (CheckBox) findViewById(R.id.CheckBox02);
				CheckBox checkbox03 = (CheckBox) findViewById(R.id.CheckBox03);
				CheckBox checkbox04 = (CheckBox) findViewById(R.id.CheckBox04);
				
				if (quiz.getAnswers().contains(0)) {
					result = result & checkbox01.isChecked();
				}
				else {
					result = result & !checkbox01.isChecked();
				}
				if (quiz.getAnswers().contains(1)) {
					result = result & checkbox02.isChecked();
				}
				else {
					result = result & !checkbox02.isChecked();
				}
				if (quiz.getAnswers().contains(2)) {
					result = result & checkbox03.isChecked();
				}
				else {
					result = result & !checkbox03.isChecked();
				}
				if (quiz.getAnswers().contains(3)) {
					result = result & checkbox04.isChecked();
				}
				else {
					result = result & !checkbox04.isChecked();
				}
				
				if (result) {
					alert("Result", "Correct!");
				}
				else {
					alert("Result", "Incorrect!");
				}
			}
			break;
		case R.id.ButtonNext:

			if ((index + 1) < channel.getQuizSize()) {
				if (channel.getQuiz(index + 1).isImageCached()) {
					index++;
					show();
				}
			}

			break;
		case R.id.ButtonPrev:

			if ((index - 1) >= 0) {
				if (channel.getQuiz(index - 1).isImageCached()) {
					index--;
					show();
				}
			}

			break;
		}
	}

	private void alert(String title, String message) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(title);
		alertDialog.setMessage(message);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				return;
			}
		});
		alertDialog.show();
	}
}